
byte address0 =    B00000000;
byte address1 =    B00000001;
byte address2 =    B00000010;
byte addressMask = B00000011;

byte on =     B00000100;
byte off =    B00000000;
byte onMask = B00000100;

int address = address1;
int ledPin = 9;
int fadeValue = 0;
boolean fadeDir = true;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    byte data = Serial.read();
    byte dataAddress = data & addressMask;
    if (dataAddress == address) {
      byte onOff = data & onMask;
      if (onOff == on) {
        digitalWrite(13, HIGH);
      } else if (onOff == off) {
        digitalWrite(13, LOW);
      }
    }
  }
  //fade();
}

void fade() {
  if (fadeValue == 255) {
    fadeDir = false;
  } else if (fadeValue == 0) {
    fadeDir = true;
  }

  analogWrite(ledPin, fadeValue);
  
  if (fadeDir) {
    fadeValue++;
  } else {
    fadeValue--;
  }
}
